// ActionScript file
package
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Button;
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	public class ScoresFormHandler
	{
		private var iScoresForm:ScoresDGridForm;
		private var iLoader:URLLoader;
		private var iTable:GenerateTable;

		private var iXMLArr:XML;
		
		public function ScoresFormHandler(aTable:GenerateTable)
		{
			iTable = aTable;
		}
			
		
		public function xmlLoad():void
		{
			iTable.TimerStop();
			
			trace("ENTER:xmlLoaded()");
			
			iLoader = new URLLoader();
			iLoader.addEventListener("complete", xmlParse);
			iLoader.load(new URLRequest("http://www.code-bg.com/alex/UserStats.xml"));
			
			trace("EXIT:xmlLoaded()");
		}
		
		public function CallScoresForm():void
		{
			trace("ENTER:CallSaveRecForm()");
			
			iScoresForm = PopUpManager.createPopUp( UIComponent( Application.application ), ScoresDGridForm, true ) as ScoresDGridForm;
			
			trace(iScoresForm);
			
			var closeBtn:Button = new Button();
			closeBtn.label = "Close";
			closeBtn.addEventListener(MouseEvent.CLICK, closeHandler);
			
			var refreshBtn:Button = new Button();
			refreshBtn.label = "Refresh";
			refreshBtn.addEventListener(MouseEvent.CLICK, refreshHandler);
			
			iScoresForm.cntrlBar.addChild(refreshBtn);
			iScoresForm.cntrlBar.addChild(closeBtn);
			PopUpManager.centerPopUp( iScoresForm );
			
			trace("EXIT:CallSaveRecForm()");
		}
		
		public function refreshHandler(event:MouseEvent):void
		{
			trace("ENTER:refreshHandler()");
			
			iScoresForm.visible = false;
			iScoresForm = PopUpManager.removePopUp(UIComponent( Application.application )) as ScoresDGridForm;
			
			xmlLoad();
			
			trace("EXIT:refreshHandler()");
		}
		
		public function closeHandler(event:MouseEvent):void
		{
			trace("ENTER:closeHandler()");
			iTable.TimerResume();
			
			iScoresForm.visible = false;
			
			trace("EXIT:closeHandler()");
		}
		
		public function xmlParse(event:Event):void
		{
			trace("ENTER:xmlParse()");
			
			iXMLArr = XML(iLoader.data);
			
			trace(iXMLArr);
			
			CallScoresForm();
			iScoresForm.xmlListCollect = new XMLListCollection( iXMLArr.Player );
			
			trace("EXIT:xmlParse()");
		}
	}
}